#include "DbPart.h"
#include "connectpool.h"
#include <sstream>
#include <vector>
#include "Game.h"
#include "Logic.h"
#include "RedisClient.h"
#include "util.h"


char g_szSQL[2056]={0};
#define MAX_SQL_BUF_LEN (sizeof(g_szSQL)-1)

CDbPart::CDbPart()
{
	m_dbHandler = NULL;
}

CDbPart::~CDbPart()
{
}

bool CDbPart::GetAgency(stAgency& agency)
{
	do
	{
		std::stringstream sql;
		sql << "select id, name, encode_key, currency, country, language, api_url, call_url, white_ips, state, agent_url, gamelist from agency ";
		if (agency.name.empty())
		{
			sql << "where id='" << agency.id << "'";
		}
		else
		{
			sql << "where name='" << agency.name << "'";
		}
		bool ret = m_dbHandler->m_pStore->Query(sql.str().c_str());
		if (!ret)
		{
			log_error("ret is falied %d, %s",ret, sql.str().c_str());
			break;
		}
		log_debug("sql:%s", sql.str().c_str());
		unsigned long count = m_dbHandler->m_pStore->RowCount();
		if (count <= 0)
		{
			log_error("count is %lu, %s",count, sql.str().c_str());
			break;
		}
		agency.id = (m_dbHandler->m_pStore->GetItemLong(0, "id"));
		agency.name = (m_dbHandler->m_pStore->GetItemString(0, "name"));
		agency.encode_key = (m_dbHandler->m_pStore->GetItemString(0, "encode_key"));
		agency.currency = (m_dbHandler->m_pStore->GetItemString(0, "currency"));
		CTools::split_str(m_dbHandler->m_pStore->GetItemString(0, "country").c_str(), ",", agency.country_list);
		CTools::split_str(m_dbHandler->m_pStore->GetItemString(0, "language").c_str(), ",", agency.language_list);
		agency.api_url = (m_dbHandler->m_pStore->GetItemString(0, "api_url"));
		agency.call_url = (m_dbHandler->m_pStore->GetItemString(0, "call_url"));
		CTools::split_str(m_dbHandler->m_pStore->GetItemString(0, "white_ips").c_str(), ",", agency.white_ips);
		agency.state = (m_dbHandler->m_pStore->GetItemLong(0, "state"));
		CTools::split_str(m_dbHandler->m_pStore->GetItemString(0, "agent_url").c_str(), ",", agency.agent_url);
		std::vector<std::string> gamelist_str_group;
		CTools::split_str(m_dbHandler->m_pStore->GetItemString(0, "gamelist").c_str(), ",", gamelist_str_group);
		for (auto item : gamelist_str_group)
		{
			agency.gamelist.emplace(::strtoul(item.c_str(), NULL, 10));
		}
		return true;
	} while(false);
	return false;
}

bool CDbPart::GetCurrency(stCurrency& st_currency)
{
	do
	{
		std::stringstream sql;
		sql << "select unit_multi, label from currency where currency='" << st_currency.currency << "'";
		bool ret = m_dbHandler->m_pStore->Query(sql.str().c_str());
		if (!ret)
		{
			log_error("ret is falied %d, %s",ret, sql.str().c_str());
			break;
		}
		log_debug("sql:%s", sql.str().c_str());
		unsigned long count = m_dbHandler->m_pStore->RowCount();
		if (count <= 0)
		{
			log_error("count is %lu, %s",count, sql.str().c_str());
			break;
		}
		st_currency.unit_multi = (m_dbHandler->m_pStore->GetItemLongLong(0, "unit_multi"));
		st_currency.label = (m_dbHandler->m_pStore->GetItemString(0, "label"));
		return true;
	} while(false);
	return false;
}
